/**********************************************************************************
 * Copyright (c) 2012 - 2012,  quick_tools, All right reserved
 * Title: com.adc.quicktools.net.http  HttpUtil.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-6-20 下午4:41:27 
 *********************************************************************************/


package com.adc.quicktools.net.http;


import java.io.BufferedOutputStream; 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.adc.quicktools.file.FileControl;





public class HttpUtil {
	
	/**
	 * 
	 * Description:   获取指定url（http发布）的文件到本地指定路径;单线程下载
	 * author:  shen
	 * @param strUrl 通过http发布的文件的url
	 * @param localPath 本地文件全路径
	 * @return boolean  
	 */
	public static boolean downloadByHttp(String strUrl, String localPath) {
		boolean b=false;
		HttpURLConnection urlconnection = null; 
		URL url = null; 
		OutputStream out=null;
		try {
			url = new URL(strUrl); 
			urlconnection = (HttpURLConnection) url.openConnection(); 
			urlconnection.connect(); 
			if (FileControl.ensurePathExist(localPath)) {
				out = new BufferedOutputStream(new FileOutputStream(localPath, false));
				b = FileControl.writeOrReadAsStream(urlconnection.getInputStream(), out);
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				urlconnection.disconnect();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return b;
	}

}
